package com.hnzbxy.api.biz.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hnzbxy.api.biz.converter.TeacherConverter;
import com.hnzbxy.api.biz.entity.Teacher;
import com.hnzbxy.api.biz.mapper.TeacherMapper;
import com.hnzbxy.api.biz.service.TeacherService;
import com.hnzbxy.api.biz.vo.TeacherVO;
import com.hnzbxy.api.common.exception.ServiceException;
import com.hnzbxy.api.system.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 教员业务逻辑实现
 * @Author xiaoge
 * @Date 2020/3/10 11:56
 * @Version 1.0
 **/
@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    /**
     * 分页+条件查询教员列表（参考UserService的Example风格）
     */
    @Override
    public PageVO<TeacherVO> findTeacherList(Integer pageNum, Integer pageSize, TeacherVO teacherVO) {
        // 1. 开启分页
        PageHelper.startPage(pageNum, pageSize);

        // 2. 构建Example查询条件
        Example example = new Example(Teacher.class);
        Example.Criteria criteria = example.createCriteria();

        // 3. 从VO中获取查询条件（与前端查询字段对应）
        String name = teacherVO.getName();
        String gender = teacherVO.getGender();
        String phone = teacherVO.getPhone();
        String email = teacherVO.getEmail();

        // 姓名模糊查询
        if (!StringUtils.isEmpty(name)) {
            criteria.andLike("name", "%" + name + "%");
        }
        // 性别精确查询
        if (!StringUtils.isEmpty(gender)) {
            criteria.andEqualTo("gender", gender);
        }
        // 手机号模糊查询
        if (!StringUtils.isEmpty(phone)) {
            criteria.andLike("phone", "%" + phone + "%");
        }
        // 邮箱模糊查询
        if (!StringUtils.isEmpty(email)) {
            criteria.andLike("email", "%" + email + "%");
        }

        // 4. 执行查询
        List<Teacher> teacherList = teacherMapper.selectByExample(example);

        // 5. 实体转VO
        List<TeacherVO> voList = TeacherConverter.converterToListVO(teacherList);

        // 6. 封装分页结果
        PageInfo<Teacher> pageInfo = new PageInfo<>(teacherList);
        return new PageVO<>(pageInfo.getTotal(), voList);
    }

    /**
     * 按条件查询所有教员（用于Excel导出）
     */
    @Override
    public List<Teacher> findAllByCondition(TeacherVO teacherVO) {
        // 复用查询条件
        Example example = new Example(Teacher.class);
        Example.Criteria criteria = example.createCriteria();

        String name = teacherVO.getName();
        String gender = teacherVO.getGender();
        String phone = teacherVO.getPhone();
        String email = teacherVO.getEmail();

        if (!StringUtils.isEmpty(name)) {
            criteria.andLike("name", "%" + name + "%");
        }
        if (!StringUtils.isEmpty(gender)) {
            criteria.andEqualTo("gender", gender);
        }
        if (!StringUtils.isEmpty(phone)) {
            criteria.andLike("phone", "%" + phone + "%");
        }
        if (!StringUtils.isEmpty(email)) {
            criteria.andLike("email", "%" + email + "%");
        }

        return teacherMapper.selectByExample(example);
    }

    /**
     * 获取所有教员列表
     */
    @Override
    public List<Teacher> findAll() {
        List<Teacher> list = teacherMapper.selectAll();
        return CollectionUtils.isEmpty(list) ? new ArrayList<>() : list;
    }

    /**
     * 新增教员
     */
    @Override
    public Teacher add(TeacherVO teacherVO) {
        // VO转实体
        Teacher teacher = TeacherConverter.converterToEntity(teacherVO);
        // 插入数据库
        int insert = teacherMapper.insert(teacher);
        if (insert != 1) {
            throw new ServiceException("新增教员失败");
        }
        return teacher;
    }

    /**
     * 删除教员
     */
    @Override
    public void delete(Long id) {
        // 先查询记录是否存在
        Teacher teacher = teacherMapper.selectByPrimaryKey(id);
        if (teacher == null) {
            throw new ServiceException("要删除的教员不存在");
        }
        // 执行删除
        int delete = teacherMapper.deleteByPrimaryKey(id);
        if (delete != 1) {
            throw new ServiceException("删除教员失败");
        }
    }

    /**
     * 获取教员详情（编辑用）
     */
    @Override
    public TeacherVO edit(Long id) {
        // 查询记录
        Teacher teacher = teacherMapper.selectByPrimaryKey(id);
        if (teacher == null) {
            throw new ServiceException("要编辑的教员不存在");
        }
        // 实体转VO
        return TeacherConverter.converterToVO(teacher);
    }

    /**
     * 更新教员信息
     */
    @Override
    public void update(Long id, TeacherVO teacherVO) {
        // 先查询记录是否存在
        Teacher dbTeacher = teacherMapper.selectByPrimaryKey(id);
        if (dbTeacher == null) {
            throw new ServiceException("要更新的教员不存在");
        }
        // VO转实体，并设置ID（确保更新指定记录）
        Teacher teacher = TeacherConverter.converterToEntity(teacherVO);
        teacher.setId(id);
        // 执行更新（选择性更新，只更新非null字段）
        int update = teacherMapper.updateByPrimaryKeySelective(teacher);
        if (update != 1) {
            throw new ServiceException("更新教员信息失败");
        }
    }

}